<?php
/**
 * gpa_new Description
 * @package
 * @name model_home
 * @filesource model_home.php
 * @author Meta Sanjaya
 */
class model_album extends Model
{
    public function  __construct() {
        parent::__construct();
    }

    public function addImageToAlbum($iid, $aid) {
        $this->db->query("INSERT INTO #_gallery_images_albums SET image_id = '$iid', album_id = '$aid'");
    }

    public function moveImageToAlbum($iid, $aid) {
        $this->db->query("UPDATE #_gallery_images_albums SET album_id = '$aid' WHERE image_id = '$iid'");
    }

    public function addAlbum($datas) {
        $album_name = $this->db->escape(htmlspecialchars($datas['album_name']));
        if(!count($this->db->query("SELECT * FROM #_users_albums WHERE user_id = '".$datas['user_id']."' AND album_name = '$album_name'")->rows)) {
            $this->db->query("INSERT INTO #_users_albums SET user_id = '".$datas['user_id']."', album_name = '$album_name', private = '".$datas['private']."'");
            return true;
        }

        return false;
    }

    public function getAlbumIdFromImageId($iid) {
        return @$this->db->query("SELECT album_id FROM #_gallery_images_albums WHERE image_id = '$iid'")->rows[0]['album_id'];
    }

    public function removeAlbum($aid) {
        $uid = $this->getUserIdFromAlbum($aid);
        $public_album = $this->getPublicAlbum($uid);
        $not_selected = $this->getNotSelectedAlbum($uid);

        if($aid == $public_album or $aid == $not_selected) {
            return false;
        }

        foreach($this->getImagesFromAlbum($aid) as $img) {
            $this->moveImageToAlbum($img, $public_album);
        }

        $this->db->query("DELETE FROM #_users_albums WHERE album_id = '$aid' AND user_id = '$uid'");

        return true;
    }

    public function getImagesFromAlbum($aid) {
        $images = array();
        $rows = $this->db->query("SELECT image_id FROM #_gallery_images_albums WHERE album_id = '$aid' ORDER BY time_stamp")->rows;

        foreach($rows as $img) {
            $images[] = $img['image_id'];
        }
        
        return $images;
    }

    public function getAlbums($uid) {
        $albums = $this->db->query("SELECT * FROM #_users_albums WHERE user_id = '$uid'")->rows;

        for($i=0,$j=count($albums);$i<$j;$i++) {
            $albums[$i]['photos_count'] = count($this->getImagesFromAlbum($albums[$i]['album_id']));
        }

        return $albums;
    }

    public function getAlbum($aid) {
        $return = array();

        if($row = @$this->db->query("SELECT * FROM #_users_albums WHERE album_id = '$aid'")->rows[0]) {
            $model_gallery = $this->getModel('gallery');
            $option['thumb_size'] = 200;
            $option['published_search'] = false;
            $photos = $this->getImagesFromAlbum($aid);
            $row['photos'] = array();
            
            foreach($photos as $photo) {
                $row['photos'][] = $model_gallery->getImageById($photo, $option);
            }
            
            $return = $row;
        }

        return $return;
    }

    public function renameAlbum($aid, $name) {
        $album_name = $this->db->escape(htmlspecialchars($name));
        if(!count($this->db->query("SELECT * FROM #_users_albums WHERE user_id = '".User::getUserId()."' AND album_name = '$album_name'")->rows)) {
            $this->db->query("UPDATE #_users_albums SET album_name = '$album_name' WHERE album_id = '$aid'");
        }
    }

    public function isUserImage($iid) {
        $rows = $this->db->query("SELECT album_id FROM #_gallery_images_albums WHERE image_id = '$iid'")->rows;
        $user_id = User::getUserId();
        
        foreach($rows as $row) {
            $aid = $row['album_id'];
            $uid = $this->getUserIdFromAlbum($aid);
            if($uid == $user_id) {
                return true;
            }
        }

        return false;
    }

    public function getPublicAlbum($uid) {
        return $this->db->query("SELECT album_id FROM #_users_albums WHERE user_id = '$uid' AND album_name = 'Public'")->rows[0]['album_id'];
    }

    public function getNotSelectedAlbum($uid) {
        return $this->db->query("SELECT album_id FROM #_users_albums WHERE user_id = '$uid' AND album_name = 'Not Selected'")->rows[0]['album_id'];
    }

    public function getUserIdFromAlbum($aid) {
        return intval(@$this->db->query("SELECT user_id FROM #_users_albums WHERE album_id = '$aid'")->rows[0]['user_id']);
    }
}
?>
